package com.liuya.safe.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.util.Properties;

/**
 * @author liuya
 * @date 20160917
 */
public class Constants{

    public static final String CURR_APP = "currApp";
    public static final String CURR_USER_TYPE = "currUserType";
    public static final String USER_FIELDS = "userFields";
    public static final String USER_FIELD_DISPLAY_NAMES = "userFieldDisplayNames";
    public static final String SHOW_USER_FIELDS = "showUserFields";
    public static final String USER_TAG_FIELD = "userTagField";
    public static final String SHOW_USER_ROLE_PANEL = "showUserRolePanel";
    public static final String SHOW_ROLE_ADMIN_PANEL = "showRoleAdminPanel";
    public static final String SHOW_POLICY_ADMIN_PANELS = "showPolicyAdminPanels";
    public static final int DEFAULT_SHOW_ENTITY_SIZE = 10;

    public static final String USER_KEY = "_$user";
    public static final String CONTEXT = "_$context";
    public static final String QUERY_MANAGER = "_$queryManager";
    public static final String SIMPLE_DATE_FORMAT = "_$simpleDateFormat";
    public static final String DOES_USER_CATEGORY_CONTAIN = "_$doesUserCategoryContain";
    public static final String IS_BUSINESS_DATA_VALID = "_$isBusinessDataValid";
    public static final String VARIABLE_MAP = "_$variableMap";
    public static final String BUSINESS_DATA = "_$businessData";

    private static String appName = "Datasafe";
//    private static String repositoryDir = "/WEB-INF/";
//    private static String businessDataStoreDir = "/WEB-INF/auth/businessData/";
//    private static String userCategoryStoreDir = "/WEB-INF/auth/userCategory/";
//    private static String queryStoreDir = "/WEB-INF/auth/query/";
//    private static String userTypeStoreDir = "/WEB-INF/auth/userType/";
    private static String repositoryDir = "/";
    private static String businessDataStoreDir = "/policy/businessData/";
    private static String userCategoryStoreDir = "/policy/userCategory/";
    private static String queryStoreDir = "/policy/query/";
    private static String userTypeStoreDir = "/policy/userType/";

    private static int queryLimit;
    private static int accountLockCount;

    public static ApplicationContext WEB_APP_CONTEXT = null; // 该值会在web容器启动时由WebAppContextListener初始化
    public static Properties prop;
    private static final String propertiesFile = "/config/custom.properties";
    private final static Logger logger = LoggerFactory.getLogger(Constants.class);

    public static final String DESIGNER = "Designer";
    public static final String NO_PRIVILEGE = "noPrivilege";
    public static final String NO_PRIVILEGE_FOR_QUERY_POLICY = "noPrivilegeForQueryPolicy";
    public static final String NO_QUERY_POLICY_MATCHED = "noQueryPolicyMatched";
    public static final String RESERVED_USER_CATEGORY_NAME = "reservedUserCategoryName";
    public static final String SAFE_ADMIN = "safeAdmin";
    public static final String POLICY_ADMIN = "policyAdmin";
    public static final String ASSIGN_ROLE_TO_USER = "AssignRoleToUser";
    public static final String ROLE_ADMIN = "RoleAdmin";
    public static final String POLICY_ADMIN_DESCRIPTION = "policyAdminDescription";
    public static final String SAFE_ADMINISTRATOR = "safeAdministrator";
    public static final String ERROR = "error";

    static{
        prop = new Properties();
        try {
            prop.load(Constants.class.getResourceAsStream(propertiesFile));
        } catch (IOException e) {
            logger.error("加载配置文件失败",e);
        }
    }

    private static Constants instance = null;
    public static Constants getInstance(){
        if(instance!=null)
            return instance;
        instance = new Constants();
        return instance;
    }

    public static int getQueryLimit() {
        return queryLimit;
    }

    public static void setQueryLimit(int queryLimit) {
        Constants.queryLimit = queryLimit;
    }

    public static String getRepositoryDir() {
        return repositoryDir;
    }

    public static void setRepositoryDir(String repositoryDir) {
        Constants.repositoryDir = repositoryDir;
    }

    public static String getBusinessDataStoreDir() {
        return businessDataStoreDir;
    }

    public static void setBusinessDataStoreDir(String businessDataStoreDir) {
        Constants.businessDataStoreDir = businessDataStoreDir;
    }

    public static String getUserCategoryStoreDir() {
        return userCategoryStoreDir;
    }

    public static void setUserCategoryStoreDir(String userCategoryStoreDir) {
        Constants.userCategoryStoreDir = userCategoryStoreDir;
    }

    public static String getQueryStoreDir() {
        return queryStoreDir;
    }

    public static void setQueryStoreDir(String queryStoreDir) {
        Constants.queryStoreDir = queryStoreDir;
    }

    public static String getUserTypeStoreDir() {
        return userTypeStoreDir;
    }

    public static void setUserTypeStoreDir(String userTypeStoreDir) {
        Constants.userTypeStoreDir = userTypeStoreDir;
    }

    public static String getAppName(){
        return appName;
    }

    public static void setAppName(String appName){
        Constants.appName = appName;
    }

    /**
     * 获取系统中配置的Integer型配置
     * 若找不到指定的配置，则返回0
     * @param name
     * @return
     */
    public static Integer getIntProperties(String name){
        String propertiesStr = prop.getProperty(name);

        return (propertiesStr!=null) ? Integer.valueOf(propertiesStr) : null;
    }

    /**
     * @param name
     * @return
     */
    public static String getProperties(String name){
        String propertiesStr = prop.getProperty(name);
        return (propertiesStr!=null) ? propertiesStr : "";
    }

    public static int getChatServerPort(){
        return getIntProperties("webscoket.port");
    }

    public static String getChatServerAddress(){
        return getProperties("webscoket.address");
    }

}
